<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;


class RoleHasPermissions extends Model
{
    protected $table = 'role_has_permissions';
    public $timestamps = true;

    /**
     * 根据角色ids 获取权限ids
     * @param $role_ids [1,2,3]
     * @return array
     */
    public static function getPermissionsByRoles($role_ids)
    {
        $rt = DB::table('role_has_permissions')->whereIn('role_id', $role_ids)->get()->toArray();
        return $rt;
    }

    /**
     * 根据用户id 获取角色
     * @param $user_id
     * @param $fields
     * @return array
     */
    public static function getRolesByUserId($user_id, $fields = ['role_id'])
    {
        $rt = DB::table('model_has_roles')->where('model_id', $user_id)->select($fields)->get()->toArray();
        return $rt;
    }

    /**
     * 根据角色ids 获取权限ids
     * @param $ids [1,2,3]
     * @param $fields [1,2,3]
     * @return array
     */
    public static function getPermissionsByIds($ids, $where = [['menu_type', '=', '1']],
                                               $fields = ['id', 'pid', 'name', 'url', 'title', 'target', 'sort', 'menu_type', 'level'])
    {
        $rt = DB::table('permissions')->whereIn('id', $ids)->where($where)
            ->orderBy('sort', 'asc')
            ->select($fields)->get()->toArray();
        return $rt;
    }

    /**
     * 获取所有菜单
     * 包含菜单和按钮
     * @return array
     */
    public static function getMenuList($where = [0,1],
        $fields = ['id', 'pid', 'name', 'url', 'title', 'target', 'sort', 'menu_type', 'level'])
    {
        $rt = DB::table('permissions')
            ->whereIn('menu_type', $where)
            ->orderBy('sort', 'asc')
            ->select($fields)->get()->toArray();
        return $rt;
    }




}
